package com.ruicar.afs.cloud.afscase.channel.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.afscase.channel.condition.DrawerPartyCondition;
import com.ruicar.afs.cloud.afscase.channel.entity.DrawerParty;
import com.ruicar.afs.cloud.afscase.channel.mapper.DrawerPartyMapper;
import com.ruicar.afs.cloud.afscase.channel.service.DrawerPartyService;
import com.ruicar.afs.cloud.afscase.channel.vo.DrawerPartyVo;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: sang jian
 * @date: 2020/5/25 14:13
 * @description:
 */
@Service
public class DrawerPartyServiceImpl extends ServiceImpl<DrawerPartyMapper, DrawerParty> implements DrawerPartyService {
    @Override
    public IPage<DrawerPartyVo> getDrawerPartyList(Page page, DrawerPartyCondition condition) {
        IPage<DrawerPartyVo> drawerPartyInfo = baseMapper.getDrawerPartyInfo(page, condition);
        ArrayList<DrawerPartyVo> carNameList = baseMapper.getCarNameList(condition);

        List<DrawerPartyVo> records = drawerPartyInfo.getRecords();

        for(int i = records.size() - 1; i >= 0; i--){
            if(StringUtils.isBlank(records.get(i).getCId())){
                records.remove(i);
                drawerPartyInfo.setTotal(drawerPartyInfo.getTotal() - 1);
            }
        }


        for (int i = 0; i < records.size(); i++) {
            for (int k = 0; k < carNameList.size(); k++) {
                if (records.get(i).getCId().equals(carNameList.get(k).getCId())) {
                    records.get(i).setCardealerId(carNameList.get(k).getCardealerId());
                    records.get(i).setCardealerName(carNameList.get(k).getCardealerName());
                    break;
                }
            }
        }
        if (StringUtils.isNotBlank(condition.getCardealerId())) {
            for (int i = records.size() - 1; i >= 0; i--) {
                if (records.get(i).getCardealerName() == null || "".equals(records.get(i).getCardealerName())) {
                    records.remove(i);
                    drawerPartyInfo.setTotal(drawerPartyInfo.getTotal() - 1);
                }
            }
        }
        return drawerPartyInfo;
    }

    @Override
    public IResponse disableDrawerParty(Long[] ids) {
        for (int i = 0; i < ids.length; i++) {
            int result = baseMapper.disableDrawerParty(ids[i]);
            if (result == 0) {
                return IResponse.fail("第" + (i + 1) + "个开票方禁用失败");
            }
        }
        return IResponse.success("禁用成功");
    }

    @Override
    public ArrayList<DrawerPartyVo> getCarDealerByChannelId(String id) {
        ArrayList<DrawerPartyVo> dealer1 = baseMapper.getCommonCarDealerByChannelId(id);
        ArrayList<DrawerPartyVo> dealer2 = baseMapper.getDirectCarDealerByChannelId(id);
        dealer2.addAll(dealer1);
        return dealer2;
    }

    @Override
    public String getChannelNameByChannelId(String id) {
        return baseMapper.getChannelNameByChannelId(id);
    }

    @Override
    public IResponse getDrawerPartyByChannel(DrawerPartyCondition condition) {
        ArrayList<DrawerParty> drawerPartyList = baseMapper.getDrawerPartyByChannel(condition);
        return IResponse.success(drawerPartyList);
    }
}
